//场内部队，指放在战役Layer上的部队

var warTroop = {
	troopHolders : [],//主管大小，位置，z顺序，并可附加其它符号
	troopSprites : [],//主管颜色，方向，振动
	
	startZIndex : 10,
	
	CreateTroop : function(){
		
		this.troopHolders = [];
		this.troopSprites = [];
		for (var troopid = 0; troopid < campaign.troop.length; troopid++){
			if (campaign.troop[troopid].strength <= 0) continue;
			
			var tileIndex = campaign.troop[troopid].tileIndex;
			
			this.troopHolders[troopid] = new cc.Node();
			this.troopHolders[troopid].attr( tileEngine.IndexToCoord(tileIndex) );
			mainMap.node.addChild(this.troopHolders[troopid], this.startZIndex + troopid);
			
			this.troopSprites[troopid] = CreateTroopSprite(troopid);
			SetTieEffect(this.troopSprites[troopid], campaign.troop[troopid].tieTimer);
			this.troopHolders[troopid].addChild(this.troopSprites[troopid], 0);
			
			var aura = CreateAuraSprite(campaign.troop[troopid].color);
			this.troopHolders[troopid].addChild(aura, -1);
			
			this.sortTileIndex = tileIndex;
			this.SortTroop();
		}
		this.RefreshTroopVisible();
	},
	
	sortTileIndex : -1,
	
	SortTroop : function(){
		if (warTroop.sortTileIndex == -1)
			return;
		
		var troopidArray = [];
		for (var troopid = 0; troopid < warTroop.troopHolders.length; troopid++){
			if (warTroop.troopHolders[troopid] == null) continue;

			if (tileEngine.CoordToIndex({x:warTroop.troopHolders[troopid].x,y:warTroop.troopHolders[troopid].y}) == warTroop.sortTileIndex){
				troopidArray[troopidArray.length] = troopid;
			}
		}

		if (troopidArray.length <= 0)
			return;

		var coord = tileEngine.IndexToCoord(warTroop.sortTileIndex);
		var distance = 0.25 * tileEngine.tileWidth;
		if (troopidArray.length == 1){
			warTroop.troopHolders[troopidArray[0]].attr(coord);
			return;
		}
		var startRad = -0.5 * Math.PI;
		var divRad = 0.5 * Math.PI;
		if (troopidArray.length > 2){
			startRad = - Math.PI / 3;
			divRad = 2 * Math.PI / troopidArray.length;
		}
		for (var idx = 0; idx < troopidArray.length; idx++){
			var rad = startRad + idx * divRad;
			warTroop.troopHolders[troopidArray[idx]].attr({
				x:coord.x + distance * Math.cos(rad),
				y:coord.y + distance * Math.sin(rad)
			});
		}
	},
	
	GetTileTroop : function(tileIndex, isBottomNotTop){
		//获取该tile上所有的sprite
		var troopidArray = [];
		for (var troopid = 0; troopid < this.troopHolders.length; troopid++){
			if (this.troopHolders[troopid] == null) continue;
			
			var tileIndex_tmp = tileEngine.CoordToIndex({x:this.troopHolders[troopid].x,y:this.troopHolders[troopid].y});
			if (tileIndex_tmp == tileIndex)
				troopidArray[troopidArray.length] = troopid;
		}
		if (troopidArray.length < 1)
			return -1;


		var tgtTroopid = -1;

		if (isBottomNotTop == false){
			//获取zIndex最大的，即最上层的sprite
			var tgtZIndex = this.startZIndex - 1;
			for (var idx = 0; idx < troopidArray.length; idx++){
				var troopid = troopidArray[idx];
				if (this.troopHolders[troopid].zIndex > tgtZIndex){
					tgtZIndex = this.troopHolders[troopid].zIndex;
					tgtTroopid = troopid;
				}
			}
		}else{
			//获取最底层的sprite
			var tgtZIndex = this.startZIndex + 89;
			for (var idx = 0; idx < troopidArray.length; idx++){
				var troopid = troopidArray[idx];
				if (this.troopHolders[troopid].zIndex < tgtZIndex){
					tgtZIndex = this.troopHolders[troopid].zIndex;
					tgtTroopid = troopid;
				}
			}
			//对所有sprite的zIndex进行排序，使选中的sprite的zIndex等于最大的zIndex，其它sprite的zIndex比它大的，则全部 -1；
			//（相当于将选中sprite的zIndex置顶了）
			var maxZIndex = this.startZIndex - 1;
			var tagZIndex = this.troopHolders[tgtTroopid].zIndex;
			for (var troopid = 0; troopid < this.troopHolders.length; troopid++){
				if (this.troopHolders[troopid] == null) continue;
				
				if (troopid == tgtTroopid)
					continue;
				
				if (this.troopHolders[troopid].zIndex > maxZIndex)
					maxZIndex = this.troopHolders[troopid].zIndex;

				if (this.troopHolders[troopid].zIndex > tagZIndex)
					this.troopHolders[troopid].zIndex--;
			}
			this.troopHolders[tgtTroopid].zIndex = maxZIndex;
		}
		return tgtTroopid;
	},
	
	ClearTroop : function(){
		for (var troopid = 0; troopid < this.troopHolders.length; troopid++)
			this.RemoveTroop(troopid);
	},
	
	RemoveTroop : function(troopid){
		if (this.troopHolders[troopid] != null){
			if (this.troopSprites[troopid] != null){
				this.troopSprites[troopid].removeFromParent();
				this.troopSprites[troopid] = null;
			}

			this.troopHolders[troopid].removeFromParent();
			this.troopHolders[troopid] = null;
		}
	},
	
	RefreshTroopVisible : function(){
		for (var troopid = 0; troopid < campaign.troop.length; troopid++){
			var sprite = warTroop.troopHolders[troopid];
			if (sprite == null) continue;
			var tileIndex = tileEngine.CoordToIndex(sprite.getPosition());
			sprite.setVisible(tileIndex >= 0 && (! warMist.mistSprites[tileIndex].isVisible()));
		}
	},
};

var CreateTroopSprite = function(troopid){
	var sprite = new cc.Sprite(campaign.troop[troopid].base_image);
	orientationEngine.SetOrientation(sprite, campaign.troop[troopid].orientation);
	return sprite;
};

var SetTieEffect = function(troopSprite, tieTimer){
	var clr = 255;
	if (tieTimer >= campaign.judgement.retreat_threshold){
		clr = 10;
	}else if (tieTimer >= campaign.judgement.hurt_threshold){
		clr = 91;
	}else if (tieTimer >= campaign.judgement.tie_threshold){
		clr = 172;
	}
	troopSprite.setColor(cc.color(clr,clr,clr));
};